import { getLineOption, getLineSeriesOption } from ".";
import { Format } from "../functions/date";
function ArrayDowngrade(array, index) {
  let data = [];
  for (let i = 0; i < array.length; i++) {
    const element = array[i];
    data.push(element[index]);
  }
  return data;
}
export function getSPOption(base, option = {}) {
  var chartOption = getLineOption();
  chartOption.title.text = "股票仓位测算图";
  chartOption.xAxis.data = ArrayDowngrade(base, 0);
  chartOption.series[0].data = ArrayDowngrade(base, 1);
  chartOption.xAxis.axisLabel.formatter = function (value, index) {
    return Format("YYYY-mm-dd", new Date(Number(value)));
  };
  (chartOption.series[0].endLabel.formatter = function (a, b, c) {
    return a.data + (option.perc ? "" : "%");
  }),
    (chartOption.yAxis.axisLabel.formatter = function (value, index) {
      return value + (option.perc ? "" : "%");
    });
  chartOption.tooltip.formatter = function (params, ticket, callback) {
    return `${params
      .map((item) => {
        return (
          item.marker +
          Format("YYYY-mm-dd", new Date(Number(item.name))) +
          " : <strong>" +
          item.value +
          "" +
          (option.perc ? "" : "%") +
          "</strong>"
        );
      })
      .join("<br/>")}`;
  };
  return chartOption;
}
export function getACWTOption(base) {
  var chartOption = getSPOption(base, { perc: true });
  chartOption.title.text = "累计净值走势";
  chartOption.dataZoom = [{ type: "inside", start: ((base.length - 50) / base.length) * 100, end: 100 }, {}];

  return chartOption;
}
export function getDGTOption(base) {
  var chartOption = getSPOption(base[0]["data"], { perc: false });
  chartOption.title.text = "累计收益率走势";
  chartOption.series = [];
  for (let i = 0; i < base.length; i++) {
    chartOption.series.push(getLineSeriesOption());
    chartOption.series[i].name = base[i].name;
    chartOption.series[i].data = ArrayDowngrade(base[i]["data"], 1);
    chartOption.series[i].endLabel.formatter = function (a, b, c) {
      return a.data + "%";
    };
    chartOption.series[i].endLabel.color = chartOption.color[i];
  }
  chartOption.tooltip.formatter = function (params, ticket, callback) {
    return (
      Format("YYYY-mm-dd", new Date(Number(params[0].name))) +
      "<br/>" +
      `${params
        .map((item) => {
          return item.marker + item.seriesName + " : <strong>" + item.value + "%</strong>";
        })
        .join("<br/>")}`
    );
  };
  return chartOption;
}

export function getJZGZOption(base) {
  var chartOption = getSPOption([[0, 0]], { perc: false });
  chartOption.title.text = "净值估算 "+Format("YYYY-mm-dd",new Date());
  chartOption.xAxis.data = ArrayDowngrade(base, "time");
  chartOption.xAxis.axisLabel.formatter = function (value, index) {
    return Format("HH:MM:SS", new Date(Number(value)));
  };
  chartOption.series[0].data = (() => {
    let data = [];
    for (let i = 0; i < base.length; i++) {
      const element = base[i];
      data.push({value:element["percentage"],nav:element["nav"]});
      if(i===base.length-1){
        if(element["percentage"]>=0){
          
          chartOption.color=["#F56C6C"];
        }else{
          
          chartOption.color=["#67C23A"];
        }
      }
    }
    return data;
  })();
  chartOption.series[0].endLabel.color = chartOption.color[0];
  chartOption.series[0].endLabel.formatter = function (a, b, c) {
    return a.data.value+"%";
    // return a.percentage + (option.perc ? "" : "%");
  }
  chartOption.tooltip.formatter = function (params, ticket, callback) {
    return `${params
      .map((item) => {
        return (
          item.marker +
          Format("HH:MM:SS", new Date(Number(item.name)))+
          "<br/>估算净值: <strong>"+item.data.nav+"</strong>"
          +"<br/>估算涨幅 : <strong>" +item.value +"%<strong/>"
        );
      })
      .join("<br/>")}`;
  };
 
  return chartOption;
}
